package org.jeecg.modules.param.fault.service.impl;


import org.apache.commons.beanutils.BeanUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.constant.BikeConstant;
import org.jeecg.modules.param.fault.entity.BikeFaultCheck;
import org.jeecg.modules.param.fault.mapper.BikeFaultCheckMapper;
import org.jeecg.modules.param.fault.service.IBikeFaultCheckService;
import org.jeecg.modules.param.fault.vo.BikeFaultCheckRes;
import org.jeecg.modules.utils.ExcelUtils;
import org.jeecg.modules.utils.RandomCodeUtils;
import org.jeecg.modules.work.repair.entity.BikeWorkRepair;
import org.jeecg.modules.work.repair.mapper.BikeWorkRepairMapper;
import org.jeecg.modules.work.sweep.entity.BikeWorkSweep;
import org.jeecg.modules.work.sweep.mapper.BikeWorkSweepMapper;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.InvocationTargetException;
import java.util.Comparator;
import java.util.List;

/**
 * @Description: 检测故障记录
 * @Author: 月月鸟
 * @Date:   2021-05-28
 * @Version: V1.0
 */
@Service
public class BikeFaultCheckServiceImpl extends ServiceImpl<BikeFaultCheckMapper, BikeFaultCheck> implements IBikeFaultCheckService {
    @Resource
    private BikeFaultCheckMapper bikeFaultCheckMapper;
    @Resource
    private BikeWorkRepairMapper bikeWorkRepairMapper;
    @Resource
    private BikeWorkSweepMapper bikeWorkSweepMapper;
    @Override
    public Result<?> pageList(Integer pageNo, Integer pageSize, BikeFaultCheckRes bikeFaultCheckRes) {
        List<BikeFaultCheckRes> bikeFaultCheckResList = bikeFaultCheckMapper.pageList(bikeFaultCheckRes);
        return Result.OK(bikeFaultCheckResList);
    }

    @Override
    public Result<?> goWorkOrder(BikeFaultCheckRes bikeFaultCheckRes) {
        BikeWorkRepair bikeWorkRepair = new BikeWorkRepair();
        if (bikeFaultCheckRes!=null){
            //单号
            bikeWorkRepair.setCode(RandomCodeUtils.getRandomCode(BikeConstant.work_dispatch_open));
            bikeWorkRepair.setStationCode(bikeFaultCheckRes.getStationCode());
            bikeWorkRepair.setStationName(bikeFaultCheckRes.getStationName());
            bikeWorkRepair.setDeviceCode(bikeFaultCheckRes.getPileCode());
            bikeWorkRepair.setAddress(bikeFaultCheckRes.getAddress());
            //故障类型(1还不了车2租不了车3网络故障4车辆损坏5车锁故障6需要保洁7其他问题)
            bikeWorkRepair.setRepairItem(bikeFaultCheckRes.getFaultType());
            //是否必须上传照片 1是 0否
            bikeWorkRepair.setIsPicture(bikeFaultCheckRes.getIsPicture());
            //工单来源 (1新增2检测故障3上报故障)
            bikeWorkRepair.setWorkSource("3");
            // TODO: 2021/6/2 负责人不确定  目前是会员列表 后期改 系统用户
            bikeWorkRepair.setUserId(bikeFaultCheckRes.getUserDutyId());
            //截止时间
            bikeWorkRepair.setFinishTime(bikeFaultCheckRes.getFinishTime());
            //是否派单 1是 0否
            bikeWorkRepair.setIsDispatch(bikeFaultCheckRes.getIsDispatch());
            //紧急程度 2 一般 1加急
            bikeWorkRepair.setUrgentType(bikeFaultCheckRes.getUrgentType());
            //备注
            bikeWorkRepair.setMemo(bikeFaultCheckRes.getMemo());
        }
        if (bikeWorkRepair.getRepairItem().equals("6")){
            BikeWorkSweep bikeWorkSweep = new BikeWorkSweep();
            //单号
            bikeWorkSweep.setCode(RandomCodeUtils.getRandomCode(BikeConstant.work_sweep_open));
            bikeWorkSweep.setStationCode(bikeFaultCheckRes.getStationCode());
            bikeWorkSweep.setStationName(bikeFaultCheckRes.getStationName());
            bikeWorkSweep.setDeviceCode(bikeFaultCheckRes.getPileCode());
            bikeWorkSweep.setAddress(bikeFaultCheckRes.getAddress());
            //故障类型(1还不了车2租不了车3网络故障4车辆损坏5车锁故障6需要保洁7其他问题)
            bikeWorkSweep.setRepairItem(bikeFaultCheckRes.getFaultType());
            //是否必须上传照片 1是 0否
            bikeWorkSweep.setIsPicture(bikeFaultCheckRes.getIsPicture());
            //工单来源 (1新增2检测故障3上报故障)
            bikeWorkSweep.setWorkSource("3");
            // TODO: 2021/6/2 负责人不确定  目前是会员列表 后期改 系统用户
            bikeWorkSweep.setUserId(bikeFaultCheckRes.getUserDutyId());
            //截止时间
            bikeWorkSweep.setFinishTime(bikeFaultCheckRes.getFinishTime());
            //是否派单 1是 0否
            bikeWorkSweep.setIsDispatch(bikeFaultCheckRes.getIsDispatch());
            //紧急程度 2 一般 1加急
            bikeWorkSweep.setUrgentType(bikeFaultCheckRes.getUrgentType());
            //备注
            bikeWorkSweep.setMemo(bikeFaultCheckRes.getMemo());
            bikeWorkSweepMapper.insert(bikeWorkSweep);
        }else {
            bikeWorkRepairMapper.insert(bikeWorkRepair);
        }
        return Result.OK("已转工单");
    }

    @Override
    public ModelAndView exportXls(HttpServletRequest request,BikeFaultCheckRes bikeFaultCheckRes) {
        return ExcelUtils.exportXls(request,bikeFaultCheckMapper.pageList(bikeFaultCheckRes),BikeFaultCheckRes.class,"检测故障记录");
    }
}
